var app = new Vue({
  el: "#shareHistory",
  data() {
    return {
      currentPage: 0,// 当前页数
      totalPage: 2, // 总页数
      loading: false, // 滚动加载更多
      keyWorld: '', // 搜索关键字
      historylist: [], // 历史列表数据
      errMsg: '', // 提示信息文字
      popShow: false, // 查看价格弹出层
      priceData: {}, // 查看价格数据
      searchOrError: 1, //1:search,2:error
      actions: [ // 价格数据
        {name: '正常价', method: this.changePriceType, value: 'NORMAL'},
        {name: '促销价', method: this.changePriceType, value: 'PROMOTION'},
        {name: '处理价', method: this.changePriceType, value: 'DEAL'},
        {name: '会员价', method: this.changePriceType, value: 'MEMBER'}],
      priceTypeVisible: false, // 价格类型弹窗层
      priceType:'', // 价格类型展示
      params: { //商品纠错数据
        supermarketId: '',
        goodsSkuId: '',
        priceType: '',
        startTime: '',
        endTime: '',
        price: '',
        file: '',
        parentId: '',
      },
      showPrice:'', // 展示价格数据
      imageSrc: '', // 纠错图片预览
    }
  },
  methods: {
    // 搜索按钮
    search() {
      this.loading = true
      this.currentPage = 1
      this.historylist = []
      this.getHistoryData()
    },
    // 滚动加载
    loadMore() {
      this.currentPage++
      if (this.currentPage > this.totalPage) {
        this.loading = true
        return false
      }
      this.getHistoryData()
    },
    // 获取分享历史记录数据
    getHistoryData () {
      Axios.post('correct/correct/searchgoodsname.do', {
        currentPage: this.currentPage,
        fullName: this.keyWorld
      }).then(res => {
        if (res.respCode == 'S') {
          let list = res.data.data
          list.forEach(item => {
            item.createTime = new Date(item.createTime).Format('yy/MM/dd')
            item.price = item.price / 100
          });
          if (this.currentPage == 1) {
            this.historylist = list
          } else {
            this.historylist = this.historylist.concat(list)
          }
          this.totalPage = res.data.totalPage
          this.currentPage = res.data.currentPage

          if (this.currentPage < this.totalPage) {
            this.loading = false
          }
        } else {
          this.historylist = []
        }
      }).catch(err => {
        console.error(err)
      })
    },
    // 撤销价格
    revokePrice (index) {
      Axios.post('correct/correct/recall.do', {
        correctId: this.historylist[index].correctId
      }).then(res => {
        if (res.respCode == 'S') {
          this.errMsg = "撤销成功！"
          setTimeout(() => {
            this.errMsg = ''
            this.historylist[index].historyButtonType = 'VIEW'
          }, 1500);
        }
      }).catch(err => {
        console.error(err)
      })
    },
    // 纠错价格
    errorCorrection (index) {
      this.searchOrError = 2
      this.params.supermarketId = this.historylist[index].supermarketId
      this.params.goodsSkuId = this.historylist[index].goodsSkuId
      this.params.parentId = this.historylist[index].correctId
    },
    // 查看价格
    viewPrice (index) {
      Axios.post('correct/correct/historyView.do', {
        correctId: this.historylist[index].correctId
      }).then(res => {
        if (res.respCode == 'S') {
          let data = res.data
          data.price = data.price / 100
          this.priceData = data
          this.popShow = true
        }
      }).catch(err => {
        console.error(err)
      })
    },
    // 关闭查看价格弹出层
    close () {
      this.popShow = false
      this.priceData = {}
    },
    // 显示时间选择器
    showPicker(key) {
      this.$refs.picker.open();
      this.timeType = key
    },
    // 时间选择器回调
    handleConfirm(data) {
      console.log(data)
      let time = data.Format('yy-MM-dd hh:mm')

      this.timeType == 1 ? this.params.startTime = time : this.params.endTime = time
    },
    // 价格类型选择
    changePriceType(k) {
        this.params.priceType = k.value
        this.priceType = k.name
    },
    // 图片预览
    showImage() {
      let inputDOM = this.$refs.file;
      // 通过DOM取文件数据
      this.params.file = inputDOM.files[0];
      var reader = new FileReader();
      reader.onload = function (evt) {
          var srcString = evt.target.result;
          //安卓获取的base64数据无信息头，加之
          if (srcString.substring(5, 10) != "image") {
              srcString = srcString.replace(/(.{5})/, "$1image/jpeg;");
          }
          app.imageSrc = srcString;
      }
      if (this.params.file) {
          reader.readAsDataURL(this.params.file);
      }
    },
    // 验证数据是否合法
    checkParam() {
      let data = this.params
      for (let i in data) {
        if (data[i] == '') {
          this.errMsg = '请填写所有项！'
          setTimeout(() => {
            this.errMsg = ''
          }, 1500);
          return false
        }
      }
      return true
    },
    // 提交价格纠错数据
    submit() {
      let data = this.params

      data.price = Math.round(this.showPrice*100)
      let param = new FormData()
      if (this.checkParam()) {
        for (let i in data) {
            param.append(i, data[i])
        }
      }
      this.errMsg = "图片正在上传中，请稍后！"
      Axios.post("correct/correct/correct.do", param)
        .then(res => { ""
          this.errMsg = res.data || res.errorMsg
            setTimeout(() => {
              if(res.respCode === 'S'){
                this.errMsg = ''
                this.searchOrError = 1
                this.imageSrc = ''
              } else {
                this.errMsg = ''
              }
            }, 1500);
        }).catch(err => {
          console.error(err)
        })
    },
  }
})
